package com.sxjh.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxjh.entity.TruckingOrderInfo;
import com.sxjh.entity.result.TruckingOrderListResult;
import com.sxjh.mapper.IntruduMapper;
import com.sxjh.mapper.TruckingOrderInfoMapper;
import com.sxjh.mapper.UnlockMapper;
import com.sxjh.service.TruckingOrderInfoService;
import com.sxjh.socket.LoadSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 定量装车信息 服务实现类
 * </p>
 *
 * @author zixin
 * @since 2023-06-19
 */
@Service
@Slf4j
public class TruckingOrderInfoServiceImpl extends ServiceImpl<TruckingOrderInfoMapper, TruckingOrderInfo> implements TruckingOrderInfoService {

    @Autowired
    private TruckingOrderInfoMapper truckingOrderInfoMapper;
    @Autowired
    private UnlockMapper unlockMapper;

    @Autowired
    private IntruduMapper intruduMapper;

    @Async(value = "taskExecutor")
    @Override
    public void pushNotice() {
        log.info("定量装车推送消息提醒");
        IPage<TruckingOrderInfo> iPage = new Page(1, 30);
        LambdaQueryWrapper<TruckingOrderInfo> queryWrapper = new LambdaQueryWrapper<>();
        //未读
        queryWrapper.eq(TruckingOrderInfo::getReaded, 2);
        //未删除
        queryWrapper.eq(TruckingOrderInfo::getDelFlag, 1);
        //|罐装值 - 计量值|>700
        queryWrapper.apply("trucking_order_details_measure !=0 and ABS(trucking_order_details_quantity - trucking_order_details_measure) > weight_error_calculation_value");
        iPage = page(iPage, queryWrapper);
        if (iPage != null && CollUtil.isNotEmpty(iPage.getRecords())) {
            List<TruckingOrderInfo> records = iPage.getRecords();
            List<TruckingOrderListResult> list = records.stream().map(this::toTruckingOrderListResult).collect(Collectors.toList());
            String message = JSON.toJSONString(list);
            try {
                LoadSocketServer.sendInfo(message, null);
                //更新为已推送
                List<TruckingOrderInfo> collect = iPage.getRecords().stream().map(this::toTruckingOrderInfoPush).collect(Collectors.toList());
                updateBatchById(collect);
            } catch (IOException e) {
                log.error("定量装车预警信息,下发异常|{}", message, e);
            }
        }
    }

    @Override
    public void receiptAll() {
        truckingOrderInfoMapper.receiptAll();
        unlockMapper.receiptAll();
        intruduMapper.receiptAll();
    }

    private TruckingOrderListResult toTruckingOrderListResult(TruckingOrderInfo info) {
        TruckingOrderListResult result = new TruckingOrderListResult();
        BeanUtils.copyProperties(info, result);
        result.setBillAt(DateUtil.formatLocalDateTime(info.getBillAt()));
        result.setCreatedAt(DateUtil.formatLocalDateTime(info.getCreatedAt()));
        result.setIsNormal(true);
        return result;
    }

    private TruckingOrderInfo toTruckingOrderInfoPush(TruckingOrderInfo info) {
        TruckingOrderInfo orderInfo = new TruckingOrderInfo();
        orderInfo.setId(info.getId());
        orderInfo.setIsPush(1);
        return orderInfo;
    }
}
